#include <allegro.h>
#include <stdio.h>

#define GFX_W 850
#define GFX_H 600

typedef struct {
float x,y;
float speed;
int direction;
BITMAP *car;
} Car ;

//utils
int get_pixel(BITMAP* Buffer,int x,int y);


//Module Background
BITMAP* load_background_bmp(char chemin_background[]);
void draw_background(BITMAP *Background,BITMAP*Buffer);
void destroy_background(BITMAP * Background);


//Module Car
void load_car(Car *car);
void destroy_car(Car *car);
void init_car(Car *car);

void move_car_forward(Car *car,int *compteur,BITMAP * Background);
void move_car_backward(Car *car,int *compteur,BITMAP * Background);
void move_car_right(Car *car,BITMAP * Background);
void move_car_left(Car *car, BITMAP * Background);
void car_brakes(Car *car, BITMAP * Background); 

void move_car(Car *car,int *compteur,BITMAP * Background);
void draw_car(Car car,BITMAP *Buffer, int compteur);

//Module car random
void load_car_random(Car *car1);
void destroy_car_random(Car *car1);
void init_car_random(Car *car1);

void move_car_random(Car *car1, int *compteur, BITMAP * Background);
void draw_car(Car car1,BITMAP *Buffer, int compteur);

int main() {

	char chemin_background[]="./images/race1.bmp";

	BITMAP *Buffer;
	BITMAP *Background=NULL;
	
	Car car;
        Car car1;

	int compteur;

	/* General initialization function */
	allegro_init();
	if (install_keyboard() == -1) {
		allegro_message("Error! %s", allegro_error);
		return (-1);
	}
	set_color_depth(16);
	if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, GFX_W, GFX_H, 0, 0) != 0) {
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); /* here’s the command for text mode */
		allegro_message("Unable to initialize video mode!\n%s\n", allegro_error);
		return (1);
	}
	Buffer = create_bitmap(SCREEN_W, SCREEN_H);
	//loading images
	//loading car
	load_car(&car);
        load_car_random(&car1);
	//loading background + background collision
	Background=load_background_bmp(chemin_background);

	//init position & direction
	compteur = 0;
	init_car(&car);
	init_car(&car1);
        

	while (!key[KEY_ESC]) {
		//vider le buffer
		clear_bitmap(Buffer);
		//moving car
		move_car(&car,&compteur,Background);
                move_car_random(&car1,&compteur,Background);
		//drawing background
		draw_background(Background,Buffer);
		draw_sprite(Buffer, car.car, car.x, car.y);
		draw_sprite(Buffer, car1.car, car1.x, car1.y);
		//drawing car
		draw_car(car,Buffer,compteur);
                draw_car(car1,Buffer,compteur);
		//blit Buffer to screen
		blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);

		if (compteur == 10000)
			compteur = 0;
	}

	//destroy Car
	destroy_car(&car);
        destroy_car_random(&car1);
	destroy_background(Background);
	destroy_bitmap(Buffer);
	return (0);
}
END_OF_MAIN();


// module background

BITMAP* load_background_bmp(char chemin_background[])
{
	BITMAP* Background;

	Background=load_bitmap(chemin_background, NULL);
	if(Background == NULL){
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
		allegro_message("Unable to load image !\n%s\n", allegro_error);
		return (NULL);
	}
	return (Background);

}


void draw_background(BITMAP *Background,BITMAP*Buffer)
{
	blit(Background, Buffer, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}

void destroy_background(BITMAP * Background)
{
	destroy_bitmap(Background);
}



//module car


void load_car(Car *car)
{
	
	car->car= load_bitmap("./images/car.bmp", NULL);                          
}

void destroy_car(Car *car)
{
        destroy_bitmap(car->car);

}


void init_car(Car *car)
{
	car->x = 0;
	car->y = 250;
	car->direction=0;
	car->speed=1;
}


void move_car_forward(Car *car,int *compteur,BITMAP * Background)
{

	if (key[KEY_RIGHT] && (car->x < SCREEN_W - car->car->w)) 
        {
		*compteur+=1;

		(car->x)+=0.3+car->speed;
		usleep(5000);
                (car->speed)+=0.1;
                car->direction == 0;
	}

}


void move_car_backward(Car *car,int *compteur,BITMAP * Background)
{

	if (key[KEY_LEFT] && (car->x > 0)) 
        {
		*compteur+=1;

		(car->x)-=1;
                car->direction == 1;
	}
}

void move_car_right(Car *car,BITMAP * Background)
{

	if (key[KEY_DOWN] && (car->y <315) ) 
        {
		(car->y)+=0.3;
                car->direction == 2;
	}

}

void move_car_left(Car *car,BITMAP * Background)
{

	if (key[KEY_UP] && (car->y > 220) ) 
        {
		(car->y)-=0.3;
                car->direction == 3;
	}

}

void car_brakes(Car *car, BITMAP * Background)
{
        int compteur;
        if (key[KEY_SPACE] && (car->x > 0 + car->car->w))
        {
          (car->speed)-=5;
          move_car_backward(car,compteur,Background);
        }
}

void move_car(Car *car,int *compteur,BITMAP * Background)
{
	move_car_forward(car,compteur,Background);
	move_car_backward(car,compteur,Background);
	move_car_right(car,Background);
        move_car_left(car,Background);
        car_brakes(car,Background);
}

void draw_car(Car car,BITMAP *Buffer, int compteur)
{
	
	if (car.direction == 0)
        { //moving forward

		draw_sprite(Buffer, car.car,car.x, car.y);

	}

	if(car.direction == 1)
        { //moving backward

		draw_sprite(Buffer, car.car,car.x, car.y);
		
	}
	if (car.direction == 2) 
        { //moving right

		draw_sprite(Buffer, car.car,car.x, car.y);
	}
	if (car.direction == 3) 
        { //moving left

		draw_sprite(Buffer, car.car,car.x, car.y);
	}
}

 //module car random

void load_car_random(Car *car1)
{
	
	car1->car= load_bitmap("./images/car1.bmp", NULL);                          
}

void destroy_car_random(Car *car1)
{
        destroy_bitmap(car1->car);

}


void init_car_random(Car *car1)
{
	car1->x = 0+(car1->car->w);
	car1->y = 400;
	car1->speed = 0.1;
}

void move_car_random(Car *car1, int *compteur, BITMAP * Background)
{
        if (car1->direction == 0)
        { 
		(car1->x)+=0.5+car1->speed;
		usleep(500);
		(car1->speed)=+0.1;
                car1->direction == 0;
	}

}

void draw_car_random(Car car1,BITMAP *Buffer, int compteur)
{
        if (car1.direction == 0) 
        {
		draw_sprite(Buffer, car1.car,car1.x, car1.y);
	}
}


